package com.zhong.gang.applib.base.fm;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import com.google.gson.Gson;
import com.zhong.gang.applib.utils.CommonTitleBar;
import com.zhong.gang.applib.R;
import com.zhong.gang.applib.base.act.ActBase;
import com.zhong.gang.applib.databinding.BaseLayoutBinding;
import com.zhong.gang.applib.rx.RxManager;

public abstract class FmBase<T extends ViewDataBinding> extends Fragment implements View.OnClickListener{
    private View view;
    private BaseLayoutBinding basebind;
    private RxManager rxManager;
    public T bind;
    public Gson gson = new Gson();
    protected ActBase mActivity ;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity= (ActBase) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getContext()).inflate(R.layout.base_layout,null);
        view.setClickable(true);
        try {
            basebind = DataBindingUtil.bind(view);
        }catch (Exception e){

        }
        getTitleBar().setVisibility(View.GONE);
        View view = LayoutInflater.from(getContext()).inflate(LayoutRes(),null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(params);
        basebind.mainlayout.addView(view);
        try {
            bind = DataBindingUtil.bind(view);
        }catch (Exception e){}
        if (showLoading()){
            showLoadingFrgamnet();
        }else {
            showMainFragemt();
        }
        basebind.errFragment.setOnClickListener(this);
        basebind.getRoot().setBackgroundColor(getResources().getColor(R.color.white));
        getTitleBar().setBackgroundColor(getResources().getColor(R.color.white));
        initView();
        return this.view;
    }

    public CommonTitleBar getTitleBar(){
        return basebind.titlebar;
    }

    public TextView setTitle(String str){
        View centerView = LayoutInflater.from(getContext()).inflate(R.layout.layout_titlebar_text,null);
        TextView textView = ((TextView)centerView.findViewById(R.id.center_tv));
        textView.setText(str);
        getTitleBar().setCenterView(centerView);
        return textView;
    }

    public View getMainlayout(){
        return  basebind.mainlayout;
    }



    public View getContentView(){
        return view;
    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.err_fragment) {
            showLoadingFrgamnet();
            getData();
        }
    }

    @LayoutRes
    public abstract int LayoutRes();
    public abstract void getData();
    public abstract void initView();
    public abstract boolean showLoading();

    public View getLoadingView(){
        return basebind.loadingFragment;
    }

    public View getErrorView(){
     return basebind.errFragment;
    }


    /**
     * 显示加载Fragment
     */
    public void showLoadingFrgamnet(){
        basebind.errFragment.setVisibility(View.GONE);
        basebind.loadingFragment.setVisibility(View.VISIBLE);
    }

    /**
     * 显示加载出错Frament
     */
    public void showErrorFragment(){
        basebind.errFragment.setVisibility(View.VISIBLE);
        basebind.loadingFragment.setVisibility(View.GONE);
    }
    /**
     * 显示主布局
     */
    public void showMainFragemt(){
        basebind.errFragment.setVisibility(View.GONE);
        basebind.loadingFragment.setVisibility(View.GONE);
    }




    /**
     * 跳转activity
     */
    public void openActivity(Class classes) {
        Intent intents = new Intent(getContext(),classes);
        startActivity(intents);
    }

    public void cloaseActivity(){
        Activity activity = getActivity();
        if (activity == null)
            return;
        if (activity instanceof ActBase){
            ((ActBase) activity).closeActivity();
        }else activity.finish();
    }

    /**
     * 跳转activity
     */
    public void openActivity(Intent intent) {
        startActivity(intent);
    }


    public RxManager getRxManager(){
        if (rxManager == null)
            rxManager = new RxManager();
        return rxManager;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (rxManager != null)
            rxManager.clear();
    }

    /**
     * 数据请求成功加载主布局
     */
    public void LoadingOver(){
        basebind.errFragment.setVisibility(View.GONE);
        basebind.loadingFragment.setVisibility(View.GONE);
        basebind.mainlayout.setVisibility(View.VISIBLE);
    }

}
